package com.cskaoyan.service.impl;

import com.cskaoyan.bean.GenericData;
import com.cskaoyan.bean.admin.AdminBo;
import com.cskaoyan.bean.admin.AdminCreateVo;
import com.cskaoyan.bean.admin.AdminListVo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.MarketAdmin;
import com.cskaoyan.bean.po.MarketAdminExample;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.service.AdminService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;

/**
 * @author LiangRuiJiang
 * @since 2022/06/05  23:25
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Override
    public GenericData queryAdminList(BaseParam param, String username) {
        PageHelper.startPage(param.getPage(), param.getLimit());


        String name = null;
        if (StringUtils.isEmpty(username)) {
            name = "%%";
        } else {
            name = "%" + username + "%";
        }

        List<AdminListVo> adminListVos = marketAdminMapper.selectAdminList(name);

        PageInfo pageInfo = new PageInfo();

        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        Integer page = param.getPage();
        Integer limit = param.getLimit();
        return GenericData.data(total, pages, limit, page, adminListVos);
    }

    @Override
    @Transactional
    public AdminCreateVo createAdmin(MarketAdmin admin) {
        //Md5加密
        admin.setPassword(DigestUtils.md5DigestAsHex(admin.getPassword().getBytes()));
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        marketAdminMapper.insertSelective(admin);
        return AdminCreateVo.data(admin);
    }

    @Override
    @Transactional
    public AdminCreateVo updateAdmin(MarketAdmin admin) {
        admin.setUpdateTime(new Date());
        MarketAdminExample example = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = example.createCriteria();

        //根据管理员id
        criteria.andIdEqualTo(admin.getId());
        String password = admin.getPassword();
        //密码MD5加密
        String passwordAfterMd5 = DigestUtils.md5DigestAsHex(password.getBytes());
        admin.setPassword(passwordAfterMd5);
        marketAdminMapper.updateByExampleSelective(admin, example);
        MarketAdmin marketAdmin = marketAdminMapper.selectByPrimaryKey(admin.getId());
        return AdminCreateVo.data(marketAdmin);
    }

    @Override
    @Transactional
    public void deleteAdmin(MarketAdmin admin) {
        //删除用户信息
        admin.setDeleted(true);

        MarketAdminExample example = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(admin.getId());

        marketAdminMapper.updateByExampleSelective(admin,example);
    }
}
